<?php

if(preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF'])) { die('You are not allowed to call this page directly.'); }

/**
 * gmAdmin - Class for admin operation
 */
class gmAdmin{

	/**
	 * wpMediaRow
	 *
	 * @param object $item
	 * @return string
	 */
	function wpMediaRow( $item ) {

		$selected_items = isset($_COOKIE['gmedia_selected_items'])? explode(',',$_COOKIE['gmedia_selected_items']) : array();
		$checked = in_array($item->ID, $selected_items)? ' checked="checked"' : '';
		$type = explode('/',$item->post_mime_type);
		$image = wp_get_attachment_image( $item->ID, array( 36, 36 ), false );
		$item_url = wp_get_attachment_url($item->ID);
		if(!$image) {
			if( $src = wp_mime_type_icon( $item->ID ) ) {
				$icon_dir_url = GRAND_URLPATH.'admin/images';
				$src_file = $icon_dir_url . '/' . wp_basename($src);
				$image = '<img src="'.$src_file.'" width="36" height="20" alt="icon" title="'.$item->post_title.'" />';
			}
		}
		$file = '<a class="grandbox" href="'.$item_url.'">'.$image.'</a>';
		$file_info = pathinfo($item_url);
		switch($type[0]){
			case 'image':
				$actions = '<a class="fancy-view" rel="gm-view" href="'.$item_url.'" title="'.__("View","gmLang").'">'.__("View","gmLang").'</a>';
			break;
			case 'audio':
				$actions = '<a class="fancy-listen" rel="gm-listen" href="'.GRAND_URLPATH.'preview.php?gmID='.$item->ID.'&amp;type='.$type[0].'" title="'.__("Listen","gmLang").'">'.__("Listen","gmLang").'</a>';
			break;
			case 'video':
				$actions = '<a class="fancy-watch" rel="gm-watch" href="'.GRAND_URLPATH.'preview.php?gmID='.$item->ID.'&amp;type='.$type[0].'" title="'.__("Watch","gmLang").'">'.__("Watch","gmLang").'</a>';
			break;
			default:
				$actions = '<a class="fancy-app" rel="gm-app" href="'.$item_url.'" title="'.__("Application","gmLang").'">'.__("Application","gmLang").'</a>';
			break;
		}
		$meta = $tip = '';
		if(get_post_meta($item->ID, '_gmedia_hidden', true)) {
			$meta = ' gmedia_hidden';
			$tip = ' title="'.__('Hidden media', 'gmLang').'"';
		}
		$trClass = $type[0].$meta;
		$actions .= '<a class="edit" href="'.admin_url('media.php?attachment_id='.$item->ID.'&amp;action=edit').'" title="'.__("Edit","gmLang").'">'.__("Edit","gmLang").'</a><a class="delete confirm" data-txt="'.__("You are about to permanently delete the selected items.\n\r'Cancel' to stop, 'OK' to delete.","gmLang").'" href="'.wp_nonce_url( "post.php?action=delete&amp;post=".$item->ID, 'delete-attachment_'.$item->ID ).'" title="'.__("Delete Permanently","gmLang").'">'.__("Delete Permanently","gmLang").'</a>';
$row = <<<EOT
<tr class="{$trClass}" id="item_{$item->ID}">
	<td class="bufer"{$tip}><span>&nbsp;</span></td>
	<td class="cb"><span><input name="doaction[]" type="checkbox" value="{$item->ID}"{$checked} /></span></td>
	<td class="id"><span>{$item->ID}</span></td>
	<td class="file"><span>{$file}</span></td>
	<td class="type"><span>{$file_info['extension']}</span></td>
	<td class="title"><span>{$item->post_title}</span></td>
	<td class="descr"><div>{$item->post_content}</div></td>
	<td class="actions"><div>{$actions}</div></td>
</tr>

EOT;

		return $row;

	}

} // END class gmAdmin

?>